package com.bos.web.action;
import com.bos.dao.search.WayBillSearchRepository;
import com.bos.domian.base.WayBill;
import com.bos.service.WayBillSearchService;
import com.bos.service.WayBillService;
import com.bos.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;

import org.apache.commons.lang3.StringUtils;

import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;


import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import java.util.HashMap;
import java.util.Map;
/**
 * @author misterWei
 * @create 2018年11月26号:10点20分
 * @mailbox mynameisweiyan@gmail.com
 */
@ParentPackage("json-default")
@Actions
@Namespace("/")
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)//多例的
@SuppressWarnings("all")
public class WayBillAction extends BaseAction<WayBill> {
@Autowired
private WayBillService wayBillService;


//搜索引擎的service
@Autowired
private WayBillSearchService wayBillSearchService;


    //分页加倒序查找
    @Action(value = "waybill_pageQuery",results = {@Result(type = "json")})
    public String wayBillPageQuery(){
        //判断当前参数是否为空走另一个逻辑


        //使用搜索引擎的方式去展示数据
        Pageable  pageable = new PageRequest(super.page-1,super.rows,new Sort(new Sort.Order(Sort.Direction.DESC,"id")));
        Map<String, Object> wayBillMap = wayBillSearchService.findAll(pageable,model);
        ActionContext.getContext().getValueStack().push(wayBillMap.get("waybillList"));
        return SUCCESS;
    }
    //保存数据
    /**
     *
     * 这个方法用来保存运单数据
     *
     */
    @Action(value = "waybill_save",results = {@Result(type = "json")})
    public String waybillSave(){
        try {
            wayBillService.save(model);
            ActionContext.getContext().getValueStack().push(true);
        }catch (Exception e){
            e.printStackTrace();
            ActionContext.getContext().getValueStack().push(false);
        }

        return SUCCESS;
    }
    /**
     * 异步加载补全运单录入表格的数据
     *
    @Action(value = "waybill_findByWayBillNum",results = {@Result(type = "json")})
    public String findBywayBillNum() {
        WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
        Map<String, Object> waybillResult = new HashMap<>(10);
        //异步加载补全运单录入表格的数据
      return SUCCESS;
    }*/
        @Action(value = "waybill_findByWayBillNum",results = {@Result(type = "json")})
    public String findBywayBillNum(){
      WayBill wayBill =   wayBillService.findByWayBillNum(model.getWayBillNum());
        Map<String,Object> waybillResult = new HashMap<>(10);
      if (wayBill == null) {
          waybillResult.put("success",false);
        }else {
          waybillResult.put("success",true);
          waybillResult.put("waybillData",wayBill);
        }
        ActionContext.getContext().getValueStack().push(waybillResult);
        return SUCCESS;
    }
}
